Skip to content

Conversation

@liuzicheng1987
Copy link
Contributor

No description provided.

@gemini-code-assist
Copy link

Summary of Changes

Hello @liuzicheng1987, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the rfl library by introducing robust support for std::atomic types. It provides new type traits and utility functions to identify and manipulate atomic variables, and integrates this functionality into the core parsing mechanism. This allows users to seamlessly serialize and deserialize data structures containing std::atomic members, ensuring thread-safe data handling without manual intervention during the (de)serialization process.

Highlights

  • New rfl::atomic Utilities: Introduced a new rfl::atomic namespace containing is_atomic, remove_atomic_t, and set_atomic to detect, strip, and safely assign values to atomic types, respectively.
  • Comprehensive is_atomic Trait: The is_atomic trait now provides specializations for std::atomic<T>, std::array<T, N>, C-style arrays T[N], std::shared_ptr<T>, std::unique_ptr<T>, rfl::Ref<T>, rfl::Box<T>, rfl::NamedTuple<Fields...>, and aggregate types, allowing for deep detection of atomic members.
  • Direct std::atomic Parsing Support: Added a specialization for rfl::parsing::Parser to directly handle std::atomic<T>, enabling seamless serialization and deserialization of atomic variables.
  • Enhanced Smart Pointer and Wrapper Parsing: Modified the parsing logic for rfl::Box, rfl::Ref, std::shared_ptr, and std::unique_ptr to correctly manage and propagate atomic properties when these types contain std::atomic members.
  • Extensive Test Coverage: Included a suite of new tests (test_atomic.cpp, test_atomic_array.cpp, test_atomic_box.cpp, test_atomic_ref.cpp, test_atomic_shared_ptr.cpp, test_atomic_unique_ptr.cpp) to validate the correct serialization and deserialization of std::atomic types in various contexts, including nested structures and smart pointers.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for std::atomic variables within the reflection-for-serialization library. The changes are extensive, adding new headers and logic to recursively handle atomic types within structs, arrays, and smart pointers. The core idea is to treat atomic variables as their underlying types during serialization and deserialization. While the overall approach is sound, I've found several issues related to handling of const-volatile qualified types which could lead to bugs or compilation errors. I've also noticed some copy-paste errors in the new test files. My review comments provide specific suggestions to address these points.

Comment on lines +45 to +47
is_atomic<T>::set(
std::forward<typename is_atomic<Type>::RemoveAtomicT>(val[i]),
&((*_t)[i]));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There's an inconsistency in the use of T versus Type (which is std::remove_cvref_t<T>). The set function uses is_atomic<T>::set, but T can have cv-qualifiers, which would cause the base template of is_atomic to be instantiated instead of the correct specialization. This can lead to incorrect behavior or compilation errors. You should consistently use Type, which is the cv-ref removed version of T.

      is_atomic<Type>::set(
          std::forward<typename is_atomic<Type>::RemoveAtomicT>(val[i]),
          &((*_t)[i]));

Comment on lines 60 to 62
is_atomic<T>::set(
std::forward<typename is_atomic<T>::RemoveAtomicT>(val[i]),
&((*_t)[i]));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The set function for C-style arrays uses is_atomic<T> instead of is_atomic<Type> (where Type is std::remove_cvref_t<T>). This is incorrect if T has any cv-qualifiers, as it will instantiate the base template of is_atomic instead of a specific specialization. This applies to both the set call and the type for std::forward.

      is_atomic<Type>::set(
          std::forward<typename is_atomic<Type>::RemoveAtomicT>(val[i]),
          &((*_t)[i]));

Comment on lines 77 to 78
is_atomic<T>::set(
std::forward<typename is_atomic<T>::RemoveAtomicT>(*val), ptr.get());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The set function for std::shared_ptr uses is_atomic<T> instead of is_atomic<Type> (where Type is std::remove_cvref_t<T>). This is incorrect if T has any cv-qualifiers, as it will instantiate the base template of is_atomic instead of a specific specialization. This applies to both the set call and the type for std::forward.

      is_atomic<Type>::set(
          std::forward<typename is_atomic<Type>::RemoveAtomicT>(*val), ptr.get());

Comment on lines 94 to 95
is_atomic<T>::set(
std::forward<typename is_atomic<T>::RemoveAtomicT>(*val), ptr.get());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The set function for std::unique_ptr uses is_atomic<T> instead of is_atomic<Type> (where Type is std::remove_cvref_t<T>). This is incorrect if T has any cv-qualifiers, as it will instantiate the base template of is_atomic instead of a specific specialization. This applies to both the set call and the type for std::forward.

      is_atomic<Type>::set(
          std::forward<typename is_atomic<Type>::RemoveAtomicT>(*val), ptr.get());

Comment on lines 128 to 144
template <class... Fields>
struct is_atomic<NamedTuple<Fields...>> {
static constexpr bool value =
(is_atomic<typename Fields::Type>::value || ...);

using RemoveAtomicT = NamedTuple<
rfl::Field<Fields::name_,
typename is_atomic<typename Fields::Type>::RemoveAtomicT>...>;

static void set(RemoveAtomicT&& val, NamedTuple<Fields...>* _t) {
(is_atomic<typename Fields::Type>::set(
std::forward<typename is_atomic<typename Fields::Type>::RemoveAtomicT>(
val.template get<Fields::name_>()),
&(_t->template get<Fields::name_>())),
...);
}
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In the specialization for NamedTuple, typename Fields::Type is used directly. If a field's type has cv-qualifiers (e.g., const std::atomic<int>), is_atomic<...> will fall back to the base template, leading to incorrect behavior. You should use std::remove_cvref_t on the field's type before passing it to is_atomic to ensure the correct specialization is chosen.

template <class... Fields>
struct is_atomic<NamedTuple<Fields...>> {
  template <class Field>
  using CleanFieldType = std::remove_cvref_t<typename Field::Type>;

  static constexpr bool value =
      (is_atomic<CleanFieldType<Fields>>::value || ...);

  using RemoveAtomicT = NamedTuple<
      rfl::Field<Fields::name_,
                 typename is_atomic<CleanFieldType<Fields>>::RemoveAtomicT>...>;

  static void set(RemoveAtomicT&& val, NamedTuple<Fields...>* _t) {
    (is_atomic<CleanFieldType<Fields>>::set(
         std::forward<typename is_atomic<
             CleanFieldType<Fields>>::RemoveAtomicT>(
             val.template get<Fields::name_>()),
         &(_t->template get<Fields::name_>())),
     ...);
  }
};

Comment on lines 8 to 19
namespace test_atomic_ref {

struct Stats {
std::atomic<std::uint64_t> bytes_downloaded;
std::atomic<bool> finished;
};

TEST(json, test_atomic_ref) {
auto stats = rfl::make_ref<Stats>(123456789, true);

write_and_read(stats, R"({"bytes_downloaded":123456789,"finished":true})");
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There seems to be a copy-paste error in this test file. The filename is test_atomic_box.cpp, but the namespace is test_atomic_ref, the test is named test_atomic_ref, and it uses rfl::make_ref which is for rfl::Ref. This test should be for rfl::Box.

namespace test_atomic_box {

struct Stats {
  std::atomic<std::uint64_t> bytes_downloaded;
  std::atomic<bool> finished;
};

TEST(json, test_atomic_box) {
  auto stats = rfl::make_box<Stats>(123456789, true);

  write_and_read(stats, R"({"bytes_downloaded":123456789,"finished":true})");
}

Comment on lines 8 to 19
namespace test_atomic_box {

struct Stats {
std::atomic<std::uint64_t> bytes_downloaded;
std::atomic<bool> finished;
};

TEST(json, test_atomic_box) {
auto stats = rfl::make_box<Stats>(123456789, true);

write_and_read(stats, R"({"bytes_downloaded":123456789,"finished":true})");
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There seems to be a copy-paste error in this test file. The filename is test_atomic_ref.cpp, but the namespace is test_atomic_box, the test is named test_atomic_box, and it uses rfl::make_box which is for rfl::Box. This test should be for rfl::Ref.

namespace test_atomic_ref {

struct Stats {
  std::atomic<std::uint64_t> bytes_downloaded;
  std::atomic<bool> finished;
};

TEST(json, test_atomic_ref) {
  auto stats = rfl::make_ref<Stats>(123456789, true);

  write_and_read(stats, R"({"bytes_downloaded":123456789,"finished":true})");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants